<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
class Mod_test extends CI_Controller {
	function __construct() {
		parent::__construct();
	}
	
	function index() {
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		if ($this->session->userdata('id')) {
			$data  = '';
			$count = 0;
			$alias = preg_replace("/(.html)/", '', $this->security->xss_clean($this->uri->segment(2, 0)));
			if ($this->session->userdata('failed') == $alias && $this->session->userdata($alias.'-time') < time()){
				$this->session->unset_userdata('failed');
				$this->session->unset_userdata($alias.'-time');
			}
			if ($this->input->post('chamdiem')) {
				$data = '';
				for ($i = 1; $i <= 10; $i++) {
					if ($this->input->post("dung$i")) {
						$id    = $this->input->post("idbaitest$i");
						$dapan = $this->common_model->getone('question_test', array(
							'id' => $id
						))->dung;
						if ($this->input->post("dung$i") == $dapan) {
							$count++;
						}
					}
				}
				if ($count >= 7) {
					$users = $this->common_model->getone('users', array(
						'id' => $this->session->userdata('id')
					), array(
						'subjects',
						'sub_test',
						'point'
					));
					if (!empty($users->sub_test)) {
						$sub_test    = $users->sub_test;
						$datasubject = $sub_test . $this->common_model->getone('question_test', array(
							'id' => $id
						))->id_subjects . ',';
					} else {
						$datasubject = ',' . $this->common_model->getone('question_test', array(
							'id' => $id
						))->id_subjects . ',';
					}
					$this->common_model->updatedata('users', array(
						'id' => $this->session->userdata('id')
					), array(
						'sub_test' => $datasubject,
						'point' => $users->point + 10
					));
					$data['content'] = $this->load->view('mod_test/views_test_success.php', array(
						'count' => $count
					), true);
				} else {
					$this->session->set_userdata('failed', $alias);
					$this->session->set_userdata($alias.'-time', (time()+7200));
					echo $this->session->userdata('failed');
					$data['content'] = $this->load->view('mod_test/views_test_wrong.php', array(
						'count' => $count
					), true);
				}
			} else {
				if($this->input->post('lambaitest')){
					$idmon = $this->common_model->getone('subjects', array(
						'alias' => $alias
					), array(
						'id',
						'title'
					));
					$this->db->like('sub_test', $idmon->id);
					$check_test = $this->common_model->getone('users', array(
						'id' => $this->session->userdata('id')
					));
					if (!empty($check_test)) {
						$data['content'] = $this->load->view('mod_test/views_test_old.php', '', true);
					} else {
						$baitest         = $this->common_model->getdata('question_test', array(
							'id_subjects' => $idmon->id
						), array(
							'id',
							'content',
							'a',
							'b',
							'c',
							'd',
							'dung'
						), array(
							'id',
							'DESC'
						), 10);
						$data['content'] = $this->load->view('mod_test/views_test.php', array(
							'baitest' => $baitest,
							'monhoc' => $idmon
						), true);
					}
				} else {
					$dat['failed'] = false;
					if(!empty($this->session->userdata('failed')) && $this->session->userdata('failed') == $alias){
						$dat['failed'] 		= true;
						$dat['nexttime'] 	= $this->session->userdata($alias.'-time');
					}
					$data['content'] = $this->load->view('mod_test/views_test_prepare.php', $dat, true);
				}
			}
			$this->load->view('index', $data);
		} else {
			redirect(base_url() . 'dang-nhap.html');
		}
	}
}
?>